#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re
from lxml import html
from html import unescape
import logging
import traceback


# /firm_ca35f035f2136edde0ce338a2e381766.html => ca35f035f2136edde0ce338a2e381766
def get_qichacha_id(task, task_str):
    task_id = task_str
    if 'type' in task:
        try:
            info = task_str.split('/')[-1]
            item = info.split('_')[-1]
            task_id = item.split('.')[0]
        except Exception as e:
            logging.error(e)
            logging.error(traceback.format_exc())
    return task_id


def split_space(task, target_str):
    if 'type' in task:
        return target_str.split('\xa0')[-1]
    return ''


def trim_html(html_label):
    pattern = re.compile(r'<[^>]+>', re.S)
    result = pattern.sub('', html_label)
    return result


# /company/1296201.html => https://data.cyzone.cn/company/1296201.html
def gen_cyzone_link(task, task_str):
    link = task_str
    if task is not None:
        link = 'https://data.cyzone.cn{0}'.format(task_str)
    return link


# /company/1296201.html => 1296201
def gen_cyzone_id(task, task_str):
    task_id = task_str
    if task is not None:
        try:
            info = task_str.split('/')[-1]
            task_id = info.split('.')[0]
        except Exception as e:
            logging.error(e)
            logging.error(traceback.format_exc())
    return task_id


def html_to_string(task, task_str):
    html_str = ''
    if task is not None:
        # 关键的一步：把整个节点转为字符串
        raw_tab = html.tostring(task_str)
        # 如果本身不是 json 字符串，则因为 unescape 函数接收的是 bytes-like 对象，所以需要先 decode
        html_str = unescape(raw_tab.decode())
    return html_str


def str_to_number(task, task_str):
    number = 0
    if 'type' in task:
        try:
            num_str = re.sub(r'\D', '', task_str)
            number = int(num_str)
        except Exception as e:
            print(e)
    return number
